<?php

namespace App\Http\Controllers;

use App\Models\News;
use Illuminate\Http\Request;

class NewsController extends Controller
{
    public function __construct(Request $request)
    {

        $user = $request->user();

        if (!$user || $user->type == 2) {
            throw new \Exception('权限不足');
        }

        parent::__construct();
    }

    public  function index(Request $request)
    {

        $user = $request->user();
        $nodes = News::with('org');
//        dd($user->platform->org_list);
        if($request->get('keyword'))
        {
            $nodes = $nodes->where('title', 'like', '%'.request()->input('keyword').'%');
        }

        $org_id = $request->get('org_id', 0);

        if ($user->type == 3) {
            $org_id = $user->org_id;
        }

        if ($user->type == 5) {
            $nodes = $nodes->where('platform_id', '=', $user->platform_id);
        }

//        dd($user);

        if ($org_id) {
            $nodes = $nodes->where('org_id', '=', $org_id);
        }

        $nodes = $nodes->orderBy('status', 'desc')->orderBy('id', 'desc');
        $info = $nodes->paginate()->toArray();
        $list = $info['data'];

        return $this->returnJSON([
            'count' => $info['total'],
            'rows' => $info['data']
        ]);
    }

    public function show($id)
    {
        $node = News::findOrFail($id);
        $data = $node->toArray();

        return $this->returnJSON($data, 0, 'ok');

    }

    public function store(Request $request)
    {
        $user = $request->user();
        $this->validate($request, [
            'title' => 'required|max:128',
            'photo' => 'required',
            'content' => 'required',
            'org_id' => $user->type == 4 ? 'required' : '',
        ]);

        $data = array_only($request->all(), ['title',  'photo', 'content', 'org_id']);
        if ($user->type === 3) {
            $data['org_id'] = $user->org_id;
        }

        if($user->type == 5)
        {
            $data['platform_id'] =$user->platform_id;
        }

        $news = new News();

        $news->fill($data);

        $news->save();

        return $this->returnJSON(null, 0, '新增成功');
    }

    public  function update(Request $request, $id)
    {
        $this->validate($request, [
            'title' => 'max:128',
            'is_recommend' => 'in:0,1',
            'status' => 'in:0,1',
        ]);

        $data = array_only($request->all(), ['title',  'photo', 'content', 'status', 'is_recommend', 'org_id']);

        $data['is_recommend'] = $request->get('is_recommend');
        $data['status'] = $request->get('status');

        $news = News::findorfail($id);

        $news->fill($data);

        $news->save();

        return $this->returnJSON(null, 0, '修改成功');
    }

    public function destroy(Request $request, $id){
        $ids = array_map(function($item) {
            return intval($item);
        }, explode(",", $id));

        if (News::whereIn('id', $ids)->delete()) {
            return $this->returnJSON($ids, 0, '删除成功');
        }
        return $this->returnJSON(null, 1, "删除失败");
    }

}
